function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 保存Base64编码图片到本地路劲
 * @param {sting || array} data 
 * @param {string} parentPath
 * @param {string} fileName 
 * @return false || string path
 */
function saveBase64Image(data, fileName){
    var save = wx.getFileSystemManager();
    path = wx.env.USER_DATA_PATH

    path = path + '/' + fileName
    console.log(path)
    try{
      save.writeFileSync(path , data, 'base64')
      return path
    }catch(e){
      console.log(e)
      wx.showToast({
        title: '请授权给我 读写权限',
        icon: 'none',
        duration: 3000
        })
    } 
    return false
}

/**
 * 以base64编码读取本地图片
 * @param {string} path
 * @return {string|ArrayBuffer|undefined}
 */
function loadBase64Image(path){
  try{
    return wx.getFileSystemManager().readFileSync(path, 'base64')
  }catch(e){
    console.log(e)
    wx.showToast({
      title: '请授权给我 读写权限',
      icon: 'none',
      duration: 3000
      })
  }
}

module.exports = {
  formatTime: formatTime,
  saveBase64Image: saveBase64Image,
  loadBase64Image: loadBase64Image
}


/* wx.getFileSystemManager().readFile({
  filePath: this.data.backwardUrl, //选择图片返回的相对路径
  encoding: 'base64', //编码格式
  success: res => { //成功的回调
    self.backwardImageData = res.data.toString()
    //console.log('Base64数据长度:', self.backwardImageData.length)
    // 显示顶部确认条
    self.setData({
      fConfirmPanel: true
    })

    // TODO: 灰度数据
    self.backwardIGraymageData = self.backwardImageData
  },
  fail: res => {
    console.log(res)
  }
}) */